package hoyo.zqy.org.service.impl;

import hoyo.zqy.basic.exception.BusinessException;
import hoyo.zqy.basic.util.BaiduAuditUtils;
import hoyo.zqy.basic.util.Md5Utils;
import hoyo.zqy.basic.util.StrUtils;
import hoyo.zqy.org.domain.Employee;
import hoyo.zqy.org.domain.Shop;
import hoyo.zqy.org.domain.ShopAuditLog;
import hoyo.zqy.org.dto.ShopDto;
import hoyo.zqy.org.mapper.EmployeeMapper;
import hoyo.zqy.org.mapper.ShopAuditLogMapper;
import hoyo.zqy.org.mapper.ShopMapper;
import hoyo.zqy.org.service.IShopService;
import hoyo.zqy.basic.service.impl.BaseServiceImpl;
import hoyo.zqy.user.domain.Logininfo;
import hoyo.zqy.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
import java.util.prefs.BackingStoreException;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //1.1.空值校验 - 前端js校验了后端也要校验，因为前端js校验不安全可以跳过
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺名称不能为空");
        }
        //店铺地址不能为空
        if (StringUtils.isEmpty(shop.getAddress())){
            throw new BusinessException("店铺地址不能为空");
        }
        //1.3.该店铺是否已经被入驻过 - 怎么判断入驻过？店铺名 + 地址
        //将传入的店铺名称在数据库内进行获取，看是否有系统名称
        Shop shopname = shopMapper.loadByName(shop);
        if (shopname!=null){
            throw new BusinessException("店铺名称或地址已经被注册");
        }
        //审核店铺名称
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称太骚，不能让你活下去了");
        }
        //审核logo
        if(!StringUtils.isEmpty(shop.getLogo()) && !BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("店铺logo太骚，虽然不能让你过但是建议发我一份");
        }
        //如果都没问题，注册成功

        //首先保存店铺管理员信息
        Employee employee = shop.getAdmin();

        //处理颜值密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.save(logininfo);

        //设置默认状态(待审核)
        employee.setState(0);
        //获取ID
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
        //其次保存店铺信息，并且将之前的店铺管理员id赋值给此店铺
        shop.setAdminId(employee.getId());
        shopMapper.save(shop);
        //在员工中更新店铺管理员id
        employee.setShop_id(shop.getId());
        employeeMapper.update(employee);
    }

    /**
     * 审核通过
     * @param log
     */
    @Override
    public void pass(ShopAuditLog log) throws MessagingException{
        //通过会修改状态
        shopMapper.updateState(log.getShopId(),2);
        //审核日志
        log.setState(2);
        shopAuditLogMapper.save(log);
        //发送邮件
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("296072843@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        //获取店铺信息
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()+"' ><a href='http://127.0.0.1:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //添加附件
        helper.addAttachment("希儿.gif",new File("C:\\Users\\hoyo\\Desktop\\图包\\希儿.gif"));
        //收件人
        //获取员工信息
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    /**
     * 审核驳回
     * @param log
     */
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //通过会修改状态
        shopMapper.updateState(log.getShopId(),4);
        //审核日志
        log.setState(4);
        shopAuditLogMapper.save(log);
        //发送邮件
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("296072843@qq.com");
        helper.setSubject("店铺激活失败");
        //true - 能够再内容中编写html标签 - 会解析
        //获取店铺信息
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>你的店铺注册失败</h1><img src='http://123.207.27.208"+shop.getLogo()+"' >",true);
        //收件人
        //获取员工信息
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    /**
     * 店铺激活
     * @param id
     * @throws BackingStoreException
     */
    @Override
    public void Active(Long id)   {
        Shop shop = shopMapper.loadById(id);
        //判断用户是否存在
        if (StringUtils.isEmpty(shop)){
            throw new BusinessException("抱歉用户不存在");
        }
        //根据id获取道用户数据
        shop.setState(3);
        shopMapper.update(shop);

        //激活店长状态
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        employee.setState(1);
        employeeMapper.update(employee);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }


}
